/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class JavaApplication2 {

    public static void main(String[] args) throws IOException
    {
        int i;
        int j;
        int k;
        int l;
        int m;
        int n;
        int o;
        int p;
        int q;
        int r;
        int s;
        int t;
        int u;
        
        BufferedWriter writer = new BufferedWriter(new FileWriter("skibolaLS.txt"));
        
        writer.append("v2.0 raw");
        writer.newLine();
        
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                for (k = 0; k < 2; k++)
                {
                    for (l = 0; l < 2; l++)
                    {
                        for (m = 0; m < 2; m++)
                        {
                            for (n = 0; n < 2; n++)
                            {
                                for (o = 0; o < 2; o++)
                                {
                                    for (p = 0; p < 2; p++)
                                    {
                                        for (q = 0; q < 2; q++)
                                        {
                                            for (r = 0; r < 2; r++)
                                            {
                                                for (s = 0; s < 2; s++)
                                                {
                                                    for (t = 0; t < 2; t++)
                                                    {
                                                        for (u = 0; u < 2; u++)
                                                        {
                                                            maquina(writer, i, j, k, l, m, n, o, p, q, r, s, t, u);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        //writer.append(":00000001FF");
        writer.close();
    }

    public static void maquina(BufferedWriter writer, int stateA, int stateB, int stateC, int timeout, int randomA, int randomB, int bolaem1, int bolaem2, int bolaem3, int bolasai, int reset, int ex1, int ex2) throws IOException
    {
        //write the binary value of the address in a string
        String str = ""+stateA+stateB+stateC+timeout+randomA+randomB+bolaem1+bolaem2+bolaem3+bolasai+reset+ex1+ex2;
        //format the binary string to a 4 digit HEX
        String formathex = str.format("%04x", Integer.parseInt(str, 2));
        //set and calculate the address part of the checksum
        int checksum = 0x1+Integer.parseInt(formathex.substring(0, 2), 16)+Integer.parseInt(formathex.substring(2, 4), 16);
        //write address part of the line
        //writer.append(":01"+formathex.toUpperCase()+"00");
        
        int led1 = 0;
        int led2 = 0;
        int led3 = 0;
        int scoreA = 0;
        int scoreB = 0;
        /*
        00=nada
        01=soma ao score
        10=reseta o score
        11=reseta timer e score
        */      
        
        if (reset == 1)
        {
            scoreA = 1;
            scoreB = 1;
            led1 = 1;
            led2 = 1;
            led3 = 1;
            
            stateA = 1;
            stateB = 1;
            stateC = 1;
        }
        else if (timeout == 1)
        {
            led1 = 1;
            led2 = 1;
            led3 = 1;
            
            stateA = 0;
            stateB = 0;
            stateC = 0;
        }
        else if (stateA == 0)
        {
            if (stateB == 0)
            {
                if (stateC == 0)
                {
                    //gameover
                }
                else
                {
                    //led1on
                    led1 = 1;
                    if (bolaem1 == 1)
                    {
                        scoreA = 1;
                        led1 = 1;
                        led2 = 1;
                        led3 = 1;
                        
                        stateA = 1;
                        stateC = 0;
                    }
                    else if ( (bolaem2 == 1) || (bolaem3 == 1) )
                    {
                        scoreB = 1;
                        
                        stateA = 1;
                    }
                }
            }
            else
            {
                if (stateC == 0)
                {
                    //led2on
                    led2 = 1;
                    if (bolaem2 == 1)
                    {
                        scoreA = 1;
                        led1 = 1;
                        led2 = 1;
                        led3 = 1;
                        
                        stateA = 1;
                        stateB = 0;
                    }
                    else if ( (bolaem1 == 1) || (bolaem3 == 1) )
                    {
                        scoreB = 1;
                        
                        stateA = 1;
                        stateB = 0;
                        stateC = 1;
                    }
                }
                else
                {
                    //led3on
                    led3 = 1;
                    if (bolaem3 == 1)
                    {
                        scoreA = 1;
                        led1 = 1;
                        led2 = 1;
                        led3 = 1;
                        
                        stateA = 1;
                        stateB = 0;
                        stateC = 0;
                    }
                    else if ( (bolaem1 == 1) || (bolaem2 == 1) )
                    {
                        scoreB = 1;
                        
                        stateA = 1;
                        stateB = 0;
                    }
                }
            }
        }
        else
        {
            if (stateB == 0)
            {
                if (stateC == 0)
                {
                    //acertou
                    led1 = 1;
                    led2 = 1;
                    led3 = 1;
                        
                    stateB = 1;
                }
                else
                {
                    //errou
                    stateB = 1;
                    stateC = 0;
                }
            }
            else
            {
                if (stateC == 0)
                {
                    //wait
                    if (bolasai == 1)
                    {
                        stateC = 1;
                    }
                }
                else
                {
                    //select
                    if (randomA == 0)
                    {
                        stateA = 0;
                        stateB = 0;
                    }
                    else
                    {
                        if (randomB == 0)
                        {
                            stateA = 0;
                            stateC = 0;
                        }
                        else
                        {
                            stateA = 0;
                        }
                    }
                }
            }
        }
        
        //write the binary value of the output in a string
        str = ""+stateA+stateB+stateC+led1+led2+led3+scoreA+scoreB;
        //format the binary string to a 2 digit HEX
        formathex = String.format("%02x", Integer.parseInt(str, 2));
        //set and calculate the output part of the checksum
        checksum += Integer.parseInt(formathex, 16);
        String formatchecksum = String.format("%02x", checksum);
        String formatchecksum2 = "-"+formatchecksum.substring(formatchecksum.length()-2, formatchecksum.length());
        String formatchecksum3 = String.format("%02x", Integer.parseInt(formatchecksum2, 16));
        //write output part of the line
        /*
        writer.append(formathex.toUpperCase()+formatchecksum3.substring(formatchecksum3.length()-2, formatchecksum3.length()).toUpperCase());
        writer.newLine();
        */
        writer.append(formathex);
        writer.newLine();
    }
}